// pages/visit/modify/modify.js
var visitservice = require("../../../apiservice/visit/visitservice");
var util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visitItem:{},
    doctorId:null,
    startDate: new Date().getTime(), //util.frmatDate(new Date()),
    visitDateShow: false,
    visitDate: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    var item =JSON.parse(options.data);
    console.log(item);
    this.setData({
      visitItem:item
    });
  },
  onMod:function(){
    
    visitservice.ModAppointment(this.data.visitItem).then(function(success){
      console.log(success);
      wx.showToast({
        title:'更新成功',
        icon:'success',
        duration:2000
      });
      var activityid = success.data.result;
      wx.navigateBack({
        url:'../detail/detail?activityid='+activityid
      });

    }).catch(function(ex){
      console.log(ex);
      wx.showToast({
        title:'更新失败',
        icon:'none',
        duration:2000
      });
      
    })
  },
/**
   * 控件赋值
   * @param {*} e 
   */
  onTimeChange:function(e){
    //console.log(e);
    this.setData({
      ['visitItem.scheduledStart'] :e.detail.value,
	  ['visitItem.scheduledEnd'] :e.detail.value
    });
    //console.log(this.data.visitItem);
  },
  onSubjectChange:function(e){
    this.setData({
      ['visitItem.subject'] :e.detail
    });
  },
  onPurposeChange:function(e){
    this.setData({
      ['visitItem.new_visitpurpose'] :e.detail
    });
    //console.log(this.data.visitItem);
  },
  toDoctorPage: function () {
    var that = this;
    wx.navigateTo({
      url: "../../doctor/list/list",
      events: {
        openedDoctorPage: function (e) {
          //console.log(e.data)
        },
        DoctorEvent: function (data) {
          console.log(data)
          var requiredattendees =[];
          requiredattendees.push(data.doctorid);
          that.setData({
            ['visitItem.requiredattendees']: requiredattendees,
            ['visitItem.requiredattendeesName']: data.doctorname
          });
        }
      },
      success: function (res) {
        res.eventChannel.emit('openedDoctorPage', { data: 'choose' })
      }
    })
  },
  selectVisitDate: function (e) {
    this.setData({ visitDateShow: true });
  },
  onVisitDateConfirm: function (e) {
    var time = e.detail;

    this.setData({
      visitDateShow: false,
      visitDate: time
    })
    var frmatDate = util.frmatDate(new Date(this.data.visitDate));
    this.setData({ ['visitItem.scheduledStart']: frmatDate, ['visitItem.scheduledEnd']: frmatDate });
  },
  onVisitCancel: function (e) {
    this.setData({ visitDateShow: false });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})